﻿using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace DiscoveryLogic.DataServices.DataSources
{
    public class SliderDateForDataBinding : INotifyPropertyChanged 
    {
        #region Public Variables
        public const string PROPERTY_SELECTED_DAY_INT = "SelectedDayInt";
        public const string PROPERTY_SELECTED_DATE_SHORT_STRING = "SelectedDateShortString";
        public const string PROPERTY_SELECTED_DATE = "SelectedDate";
        public const string PROPERTY_TOTAL_AFTER_PAYMENT = "TotalAfterPayment";
        public const string PROPERTY_TOTAL_PAYABLE_BY_DATE = "TotalPayableByDate";

        #endregion

        #region Private Variables

        private const int TOTAL_SLIDER_DAYS = 30;

        private int selectedDay = 0;
        private int minDay = 0;
        private int maxDay = TOTAL_SLIDER_DAYS;
        private DateTime baseDate = DateTime.Now.Date;
        private List<BillPayeeForDataBinding> listOfBillPayees;
        private double totalInAccountOnDate;
        private double acctFunds;
        private double totalPayableByDate;

        #endregion

        #region Constructor

        public SliderDateForDataBinding(List<BillPayeeForDataBinding> listOfPayees, double accountFunds)
        {
            listOfBillPayees = listOfPayees;
            acctFunds = accountFunds;
        }
        
        #endregion

        #region Get/Set methods

        public string SelectedDateShortString
        {
            get { return SelectedDate.ToString("MMM d, yyyy"); }
        }

        public DateTime SelectedDate
        {
            get { return baseDate.AddDays(selectedDay); }
        }

        public int MinDayInt
        {
            get { return minDay; }
        }

        public int MaxDayInt
        {
            get { return maxDay; }
        }

        public int SelectedDayInt
        {
            get { return selectedDay; }
            set
            {
                selectedDay = value;

                //recalculate total payable since new day is selected
                RecalculateAmounts();

                NotifyPropertyChanged(PROPERTY_SELECTED_DAY_INT);
                NotifyPropertyChanged(PROPERTY_SELECTED_DATE_SHORT_STRING);
                NotifyPropertyChanged(PROPERTY_SELECTED_DATE);
            }
        }

        public double TotalAfterPayment
        {
            get { return acctFunds - TotalPayableByDate; }
        }
        
        public double TotalPayableByDate
        {
            get { return totalPayableByDate; }
            set
            {
                totalPayableByDate = value;
                NotifyPropertyChanged(PROPERTY_TOTAL_PAYABLE_BY_DATE);
                NotifyPropertyChanged(PROPERTY_TOTAL_AFTER_PAYMENT);
            }
        }

        #endregion

        #region Public Methods

        public void RecalculateAmounts()
        {
            double currTotalPayable = TotalPayableByDate;
            double total = 0;
            foreach (BillPayeeForDataBinding billPayee in listOfBillPayees)
            {
                DateTime withdrawlDate = billPayee.WithdrawlDate;
                double amount = billPayee.Amount;
                if (SelectedDate >= withdrawlDate)
                {
                    total += amount;
                }
            }

            double newTotalAfterPayment = acctFunds - total;
            //Do not raise property changed events if no change
            if (total != currTotalPayable)
            {
                TotalPayableByDate = total;
            }
        }

        #endregion

        #region Public INotifyPropertyChanged Events
        public event PropertyChangedEventHandler PropertyChanged;

        //Call event to notify UI of property change
        public void NotifyPropertyChanged(string propertyName)
        {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
        }
        #endregion
    }
}
